package com.dnzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.ISubCategoryDao;
import com.dnzx.model.SubCategoryBO;
import com.dnzx.service.ISubCategoryService;

/**
 * 商品分类
 */
@Service
public class SubCategoryServiceImpl implements ISubCategoryService {

    @Resource
    private ISubCategoryDao subCategoryDao;

    @Override
    public void save(SubCategoryBO subCategoryBO) throws Exception {
    	// 要先删除之前的配置，再保存新的配置
    	this.subCategoryDao.deleteByCategoryId(subCategoryBO.getCategoryId());
    	List<String> subCategoryList = subCategoryBO.getSubCategoryList();
    	List<Double> subCategoryPriceList = subCategoryBO.getSubCategoryPriceList();
    	if (subCategoryList != null && subCategoryList.size() > 0) {
    		SubCategoryBO subCategoryBODb = null;
    		for (int i = 0; i < subCategoryList.size(); i++) {
    			double price = subCategoryPriceList.get(i);
    			subCategoryBODb = new SubCategoryBO();
    			subCategoryBODb.setCategoryId(subCategoryBO.getCategoryId());
    			subCategoryBODb.setCode(subCategoryList.get(i));
    			subCategoryBODb.setPrice(price);
    			String doorCountStr = subCategoryList.get(i)
    					.replaceAll("门", "")
    					.replaceAll("doors", "")
    					.replaceAll("door", "")
    					.replaceAll(" ", "");
    			int doorCount = new Integer(doorCountStr);
    			subCategoryBODb.setDoorCount(doorCount);
    			this.subCategoryDao.save(subCategoryBODb);
    		}
    	}
    }

    @Override
    public void delete(Long id) throws Exception {
        this.subCategoryDao.delete(id);
    }

    @Override
    public void update(SubCategoryBO subCategoryBO) throws Exception {
        this.subCategoryDao.update(subCategoryBO);
    }
    
    @Override
    public SubCategoryBO get(Long id) {
        return this.subCategoryDao.get(id);
    }
    
    @Override
	public SubCategoryBO get(SubCategoryBO subCategoryBO) {
    	List<SubCategoryBO> subCategoryBOList = this.subCategoryDao.queryList(subCategoryBO);
    	if (subCategoryBOList != null && subCategoryBOList.size() > 0) {
    		return subCategoryBOList.get(0);
    	}
		return null;
	}
    
    @Override
    public List<SubCategoryBO> queryList(SubCategoryBO subCategoryBO) {
        return this.subCategoryDao.queryList(subCategoryBO);
    }

    @Override
    public PageResult<List<SubCategoryBO>> queryPage(SubCategoryBO subCategoryBO, PageResult<List<SubCategoryBO>> pageResult) {
    	int totalRows = this.subCategoryDao.queryCount(subCategoryBO);
    	pageResult.setTotalRows(totalRows);
    	pageResult.setCurrentPage(subCategoryBO.getCurrentPage());
    	pageResult.setPageSize(subCategoryBO.getPageSize());
    	List<SubCategoryBO> records = this.subCategoryDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), subCategoryBO);
    	pageResult.setT(records);
    	return pageResult;
    }
    
}
